package arduino.controller.view;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
import org.rxtx.SerialCommunicationManager;

import commons.utils.StringUtils;

/**
 * This view shows a &quot;mail message&quot;. This class is contributed through
 * the plugin.xml.
 */
public class ArduinoCommunicationView extends ViewPart {

    public static final String ID = "arduino.controller.view";
    private final SerialCommunicationManager twoWaySerialComm = new SerialCommunicationManager();
    private Text _command;
    private Text _console;

    public void createPartControl(Composite parent) {
	Composite top = new Composite(parent, SWT.NONE);
	GridLayout layout = new GridLayout(3, false);
	layout.marginHeight = 0;
	layout.marginWidth = 0;
	top.setLayout(layout);
	// top banner
	Composite banner = new Composite(top, SWT.NONE);
	banner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));
	layout = new GridLayout();
	layout.marginHeight = 5;
	layout.marginWidth = 10;
	banner.setLayout(layout);

	// setup bold font
	Font boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);

	Label l = new Label(banner, SWT.WRAP);
	l.setText("Arduino serial communication development");
	l.setFont(boldFont);
	l.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

	final Button checkbox = new Button(top, SWT.CHECK);
	checkbox.setText("Say \"Hello Arduino\":");
	checkbox.addSelectionListener(new SelectionAdapter() {
	    public void widgetSelected(SelectionEvent event) {
		if (checkbox.getSelection()) {
		    Display.getCurrent().asyncExec(new Runnable() {
			public void run() {
			    try {
				twoWaySerialComm.connect("/dev/ttyUSB0");
				twoWaySerialComm.setRequest("Hello Arduino");
				twoWaySerialComm.communicate();
			    } catch (Exception e) {
				e.printStackTrace();
				twoWaySerialComm.closeConnection();
			    }
			}
		    });
		} else {
		    twoWaySerialComm.closeConnection();
		}
	    }
	});
	checkbox.setLayoutData(new GridData(SWT.LEAD, SWT.FILL, false, false, 3, 1));

	l = new Label(top, SWT.WRAP);
	l.setText("Command in format 'command=value' :");
	l.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
	_command = new Text(top, SWT.BORDER);
	_command.setText("");
	_command.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
	Button submit = new Button(top, SWT.PUSH);
	submit.setText("Submit");
	submit.setLayoutData(new GridData(SWT.LEAD, SWT.TOP, false, false));
	submit.addSelectionListener(new SelectionAdapter() {
	    public void widgetSelected(SelectionEvent e) {
		String command = _command.getText();
		if (StringUtils.isNotBlank(command)) {
		    _command.setText("");
		    String console = _console.getText();
		    console = command + "\n" + console;
		    _console.setText(console);
		}
	    }
	});
	// message contents
	_console = new Text(top, SWT.MULTI | SWT.WRAP);
	_console.setText("");
	_console.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
	_console.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
	_console.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
    }

    public void setFocus() {
    }
}
